using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class ToolRecycler : MonoBehaviour
{
	public float recycleRate;

	public float detectRadius = 0.3f;

	public LayerMask toolLayer;

	public Gradient flashColor;

	public float flashDuration = 1f;

	public Canvas buttonUi;

	public AudioClip recycleFailSound;

	public AudioClip recycleSound;

	private bool checkingEnabled;

	private float spriteFlashFrame;

	private Tool selectedTool;

	private SpriteRenderer[] selectedToolSprites;

	private Tool frameSelectedTool;

	private Canvas buttonUiInstance;

	private void Start()
	{
		checkingEnabled = true;
		if (!buttonUiInstance)
		{
			buttonUiInstance = Object.Instantiate(buttonUi);
			buttonUiInstance.enabled = false;
			buttonUiInstance.sortingOrder = 100;
			buttonUiInstance.GetComponentInChildren<Button>().onClick.AddListener(Uninstall);
		}
	}

	private void Uninstall()
	{
		CampaignGame.Instance.player.StartAttack();
		Invoke("UninstallComplate", 0.5f);
	}

	private void UninstallComplate()
	{
		if (!selectedTool)
		{
			return;
		}
		foreach (StageItem stageItem in DO.Campaign.stageItems)
		{
			if (stageItem.pos == (Vector2)selectedTool.transform.position)
			{
				DO.Campaign.stageItems.Remove(stageItem);
				break;
			}
		}
		RecycleItem();
		UnselectTool();
	}

	private void RecycleItem()
	{
		bool num = recycleRate >= Random.value;
		if (!num && !CampaignGame.Instance.isPlayground)
		{
			selectedTool.HitEffect();
			AudioSource.PlayClipAtPoint(recycleFailSound, selectedTool.transform.position);
		}
		Object.Destroy(selectedTool.gameObject);
		if (!num || CampaignGame.Instance.isPlayground)
		{
			return;
		}
		AudioSource.PlayClipAtPoint(recycleSound, selectedTool.transform.position);
		string key = selectedTool.name.Replace("(Clone)", "");
		DO.Campaign.availableItems.Add(key);
		if (key == "Turret" && DO.Campaign.availableItems.Count((string s) => s == key) > 1)
		{
			int num2 = DO.Campaign.availableItems.IndexOf("Turret");
			if (num2 != -1)
			{
				DO.Campaign.availableItems.RemoveAt(num2);
			}
		}
		Button button = null;
		foreach (Transform item in CampaignGame.Instance.itemListView.transform)
		{
			if (item.name == key)
			{
				button = item.GetComponent<Button>();
				break;
			}
		}
		if (!button)
		{
			button = CampaignGame.Instance.AddItemButtonToListView(CampaignGame.itemCache[key]);
		}
		int num3 = DO.Campaign.availableItems.Where((string s) => s == key).Count();
		if (num3 > 0)
		{
			button.GetComponent<Image>().color = Color.black;
		}
		button.transform.Find("Count").GetComponent<Text>().text = num3.ToString();
	}

	private void SelectTool(Tool newTool)
	{
		if (selectedTool != null && selectedTool != newTool)
		{
			UnselectTool();
		}
		selectedTool = newTool;
		selectedToolSprites = newTool.GetComponentsInChildren<SpriteRenderer>();
		spriteFlashFrame = 0f;
		buttonUiInstance.transform.position = selectedTool.transform.position;
		buttonUiInstance.enabled = true;
	}

	private void UnselectTool()
	{
		if (selectedToolSprites != null && selectedToolSprites.Length != 0)
		{
			SpriteRenderer[] array = selectedToolSprites;
			for (int i = 0; i < array.Length; i++)
			{
				array[i].color = Color.white;
			}
		}
		frameSelectedTool = null;
		selectedTool = null;
		selectedToolSprites = null;
		spriteFlashFrame = 0f;
		buttonUiInstance.enabled = false;
	}

	private void UpdateSelectedTool()
	{
		if ((bool)selectedTool && selectedToolSprites.Length != 0)
		{
			spriteFlashFrame += Time.fixedDeltaTime;
			SpriteRenderer[] array = selectedToolSprites;
			for (int i = 0; i < array.Length; i++)
			{
				array[i].color = flashColor.Evaluate(spriteFlashFrame / flashDuration);
			}
			if (flashDuration <= spriteFlashFrame)
			{
				spriteFlashFrame = 0f;
			}
		}
	}

	private void DetectTool()
	{
		if (!checkingEnabled)
		{
			return;
		}
		RaycastHit2D[] array = Physics2D.CircleCastAll(CampaignGame.Instance.player.transform.position, detectRadius, Vector3.forward, float.PositiveInfinity, toolLayer);
		if (array.Length == 0 && selectedTool != null)
		{
			UnselectTool();
			return;
		}
		float num = float.PositiveInfinity;
		Tool tool = null;
		RaycastHit2D[] array2 = array;
		for (int i = 0; i < array2.Length; i++)
		{
			RaycastHit2D raycastHit2D = array2[i];
			if (!(raycastHit2D.collider != null))
			{
				continue;
			}
			Tool component = raycastHit2D.collider.GetComponent<Tool>();
			if (!(component != null))
			{
				continue;
			}
			tool = component;
			if ((bool)tool && tool.placed)
			{
				float num2 = Vector2.Distance(raycastHit2D.collider.transform.position, CampaignGame.Instance.player.transform.position);
				if (num2 < num)
				{
					num = num2;
					frameSelectedTool = tool;
				}
			}
		}
		if (tool == null)
		{
			UnselectTool();
		}
		else if (frameSelectedTool != null && frameSelectedTool != selectedTool)
		{
			SelectTool(frameSelectedTool);
		}
	}

	public bool IsUsing()
	{
		if (CampaignGame.Instance.player.meleeWeapon == null)
		{
			return false;
		}
		if (CampaignGame.Instance.player.meleeWeapon.gameObject != base.gameObject)
		{
			return false;
		}
		return true;
	}

	private void FixedUpdate()
	{
		if (IsUsing())
		{
			DetectTool();
			UpdateSelectedTool();
		}
		else if ((bool)selectedTool)
		{
			UnselectTool();
		}
	}
}
